package com.ruoyi.kjcg.qygfpw.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.kjcg.qygfpw.domain.QygfpwFj;
import com.ruoyi.kjcg.qygfpw.mapper.QygfpwMapper;
import com.ruoyi.kjcg.qygfpw.domain.Qygfpw;
import com.ruoyi.kjcg.qygfpw.service.IQygfpwService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 企业工法批文Service业务层处理
 *
 * @author huacai
 * @date 2024-11-05
 */
@Service
public class QygfpwServiceImpl implements IQygfpwService {
    @Resource
    private QygfpwMapper qygfpwMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询企业工法批文
     *
     * @param qygfpwId 企业工法批文主键
     * @return 企业工法批文
     */
    @Override
    public Qygfpw selectQygfpwByQygfpwId(String qygfpwId) {
        return qygfpwMapper.selectQygfpwByQygfpwId(qygfpwId);
    }

    /**
     * 查询企业工法批文列表
     *
     * @param qygfpw 企业工法批文
     * @return 企业工法批文
     */
    @Override
    public List<Qygfpw> selectQygfpwList(Qygfpw qygfpw) {
        return qygfpwMapper.selectQygfpwList(qygfpw);
    }

    /**
     * 新增企业工法批文
     *
     * @param qygfpw 企业工法批文
     * @return 结果
     */
    @Transactional
    @Override
    public int insertQygfpw(Qygfpw qygfpw) {
        qygfpw.setCreateTime(DateUtils.getNowDate());
        int rows = qygfpwMapper.insertQygfpw(qygfpw);
        insertQygfpwFj(qygfpw);
        return rows;
    }

    /**
     * 批量新增企业工法批文
     *
     * @param qygfpws 企业工法批文List
     * @return 结果
     */
    @Override
    public int batchInsertQygfpw(List<Qygfpw> qygfpws) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(qygfpws)) {
            try {
                for (int i = 0; i < qygfpws.size(); i++) {
                    int row = qygfpwMapper.insertQygfpw(qygfpws.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == qygfpws.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改企业工法批文
     *
     * @param qygfpw 企业工法批文
     * @return 结果
     */
    @Transactional
    @Override
    public int updateQygfpw(Qygfpw qygfpw) {
        qygfpw.setUpdateTime(DateUtils.getNowDate());
//        qygfpwMapper.deleteQygfpwFjByQygfpwId(qygfpw.getQygfpwId());
        insertQygfpwFj(qygfpw);
        return qygfpwMapper.updateQygfpw(qygfpw);
    }

    /**
     * 批量删除企业工法批文
     *
     * @param qygfpwIds 需要删除的企业工法批文主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQygfpwByQygfpwIds(String[] qygfpwIds) {
        qygfpwMapper.deleteQygfpwFjByQygfpwIds(qygfpwIds);
        return qygfpwMapper.deleteQygfpwByQygfpwIds(qygfpwIds);
    }

    /**
     * 删除企业工法批文信息
     *
     * @param qygfpwId 企业工法批文主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQygfpwByQygfpwId(String qygfpwId) {
        qygfpwMapper.deleteQygfpwFjByQygfpwId(qygfpwId);
        return qygfpwMapper.deleteQygfpwByQygfpwId(qygfpwId);
    }

    /**
     * 新增附件
     * @param qygfpwFj
     * @return
     */
    @Override
    public int insertQygfpwFjByQygfpwId(QygfpwFj qygfpwFj) {
        return qygfpwMapper.insertQygfpwFjByQygfpwId(qygfpwFj);
    }

    /**
     * 根据ID查询附件列表
     * @param qygfpwFj
     * @return
     */
    @Override
    public List<QygfpwFj> selectQygfpwFjList(QygfpwFj qygfpwFj) {
        return qygfpwMapper.selectQygfpwFjList(qygfpwFj);
    }

    /**
     * 批量删除企业工法批文附件
     *
     * @param fjIds 需要删除的企业工法批文附件主键
     * @return 结果
     */
    @Override
    public int deleteQygfpwFjByFjIds(Integer[] fjIds)
    {
        return qygfpwMapper.deleteQygfpwFjByFjIds(fjIds);
    }
    /**
     * 新增企业工法批文附件信息
     *
     * @param qygfpw 企业工法批文对象
     */
    public void insertQygfpwFj(Qygfpw qygfpw) {
        List<QygfpwFj> qygfpwFjList = qygfpw.getQygfpwFjList();
        String qygfpwId = qygfpw.getQygfpwId();
        if (StringUtils.isNotNull(qygfpwFjList)) {
            List<QygfpwFj> list = new ArrayList<QygfpwFj>();
            for (QygfpwFj qygfpwFj : qygfpwFjList) {
                qygfpwFj.setQygfpwId(qygfpwId);
                list.add(qygfpwFj);
            }
//            if (list.size() > 0) {
//                qygfpwMapper.batchQygfpwFj(list);
//            }
        }
    }
}
